
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.vis.structure_chemview &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.vis.structure_chemview</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.vis.structure_chemview</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Visualization for structures using chemview.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.molecule_structure_comparator</span> <span class="kn">import</span> <span class="n">CovalentRadius</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">requires</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">chemview</span> <span class="kn">import</span> <span class="n">MolecularViewer</span>
    <span class="kn">from</span> <span class="nn">chemview.utils</span> <span class="kn">import</span> <span class="n">get_atom_color</span>

    <span class="n">chemview_loaded</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">chemview_loaded</span> <span class="o">=</span> <span class="kc">False</span>


<div class="viewcode-block" id="quick_view"><a class="viewcode-back" href="../../../pymatgen.vis.structure_chemview.html#pymatgen.vis.structure_chemview.quick_view">[docs]</a><span class="nd">@requires</span><span class="p">(</span><span class="n">chemview_loaded</span><span class="p">,</span> <span class="s2">&quot;To use quick_view, you need to have chemview installed.&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">quick_view</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">bonds</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">conventional</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">transform</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">show_box</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">bond_tol</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span>
               <span class="n">stick_radius</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A function to visualize pymatgen Structure objects in jupyter notebook using chemview package.</span>

<span class="sd">    Args:</span>
<span class="sd">        structure: pymatgen Structure</span>
<span class="sd">        bonds: (bool) visualize bonds. Bonds are found by comparing distances</span>
<span class="sd">                        to added covalent radii of pairs. Defaults to True.</span>
<span class="sd">        conventional: (bool) use conventional cell. Defaults to False.</span>
<span class="sd">        transform: (list) can be used to make supercells with pymatgen.Structure.make_supercell method</span>
<span class="sd">        show_box: (bool) unit cell is shown. Defaults to True.</span>
<span class="sd">        bond_tol: (float) used if bonds=True. Sets the extra distance tolerance when finding bonds.</span>
<span class="sd">        stick_radius: (float) radius of bonds.</span>
<span class="sd">    Returns:</span>
<span class="sd">        A chemview.MolecularViewer object</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">s</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">conventional</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">get_conventional_standard_structure</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">transform</span><span class="p">:</span>
        <span class="n">s</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">(</span><span class="n">transform</span><span class="p">)</span>
    <span class="n">atom_types</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">symbol</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">species</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">bonds</span><span class="p">:</span>
        <span class="n">bonds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">num_sites</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">sym_i</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">num_sites</span><span class="p">):</span>
                <span class="n">sym_j</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span>
                <span class="n">max_d</span> <span class="o">=</span> <span class="n">CovalentRadius</span><span class="o">.</span><span class="n">radius</span><span class="p">[</span><span class="n">sym_i</span><span class="p">]</span> <span class="o">+</span> <span class="n">CovalentRadius</span><span class="o">.</span><span class="n">radius</span><span class="p">[</span><span class="n">sym_j</span><span class="p">]</span> <span class="o">+</span> <span class="n">bond_tol</span>
                <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">get_distance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]))</span> <span class="o">&lt;</span> <span class="n">max_d</span><span class="p">:</span>
                    <span class="n">bonds</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">))</span>
    <span class="n">bonds</span> <span class="o">=</span> <span class="n">bonds</span> <span class="k">if</span> <span class="n">bonds</span> <span class="k">else</span> <span class="kc">None</span>

    <span class="n">mv</span> <span class="o">=</span> <span class="n">MolecularViewer</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">cart_coords</span><span class="p">,</span> <span class="n">topology</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;atom_types&#39;</span><span class="p">:</span> <span class="n">atom_types</span><span class="p">,</span> <span class="s1">&#39;bonds&#39;</span><span class="p">:</span> <span class="n">bonds</span><span class="p">})</span>

    <span class="k">if</span> <span class="n">bonds</span><span class="p">:</span>
        <span class="n">mv</span><span class="o">.</span><span class="n">ball_and_sticks</span><span class="p">(</span><span class="n">stick_radius</span><span class="o">=</span><span class="n">stick_radius</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">sites</span><span class="p">:</span>
        <span class="n">el</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span>
        <span class="n">coord</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">coords</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">CovalentRadius</span><span class="o">.</span><span class="n">radius</span><span class="p">[</span><span class="n">el</span><span class="p">]</span>
        <span class="n">mv</span><span class="o">.</span><span class="n">add_representation</span><span class="p">(</span><span class="s1">&#39;spheres&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;coordinates&#39;</span><span class="p">:</span> <span class="n">coord</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">),</span>
                                          <span class="s1">&#39;colors&#39;</span><span class="p">:</span> <span class="p">[</span><span class="n">get_atom_color</span><span class="p">(</span><span class="n">el</span><span class="p">)],</span>
                                          <span class="s1">&#39;radii&#39;</span><span class="p">:</span> <span class="p">[</span><span class="n">r</span> <span class="o">*</span> <span class="mf">0.5</span><span class="p">],</span>
                                          <span class="s1">&#39;opacity&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">})</span>
    <span class="k">if</span> <span class="n">show_box</span><span class="p">:</span>
        <span class="n">o</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
        <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">s</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">s</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="n">starts</span> <span class="o">=</span> <span class="p">[</span><span class="n">o</span><span class="p">,</span> <span class="n">o</span><span class="p">,</span> <span class="n">o</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">]</span>
        <span class="n">ends</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">b</span> <span class="o">+</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">a</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">]</span>
        <span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="mh">0xffffff</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">12</span><span class="p">)]</span>
        <span class="n">mv</span><span class="o">.</span><span class="n">add_representation</span><span class="p">(</span><span class="s1">&#39;lines&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;startCoords&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">starts</span><span class="p">),</span>
                                        <span class="s1">&#39;endCoords&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">ends</span><span class="p">),</span>
                                        <span class="s1">&#39;startColors&#39;</span><span class="p">:</span> <span class="n">colors</span><span class="p">,</span>
                                        <span class="s1">&#39;endColors&#39;</span><span class="p">:</span> <span class="n">colors</span><span class="p">})</span>
    <span class="k">return</span> <span class="n">mv</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.vis.structure_chemview</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>